package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketKeyword;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.KeywordService;
import net.sf.jsqlparser.statement.select.First;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Since 2022/12/29 10:48
 * @Author LA
 * @Description: 处理关键词相关的业务
 */
@RestController
@RequestMapping("admin/keyword")
public class KeywordController {

    @Autowired
    KeywordService keywordService;

    // http://182.92.235.201:8083/admin/keyword/list?page=1&limit=20&keyword=te&url=1&sort=add_time&order=desc
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, String keyword, String url) {
        CommonData<MarketKeyword> data = keywordService.list(info, keyword, url);
        return BaseRespVo.ok(data);
    }

    //
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody MarketKeyword marketKeyword) {
        // {keyword: "La关键词", url: "tiaozhuanlianjie", isHot: true, isDefault: true}
        marketKeyword.setAddTime(new Date());
        marketKeyword.setUpdateTime(new Date());
        // 调用 新增关键词方法
        keywordService.create(marketKeyword);
        // 响应对象
        // {
        // 	"errno": 0,
        // 	"data": {
        // 		"id": 47,
        // 		"keyword": "La关键词",
        // 		"url": "tiaozhuanlianjie",
        // 		"isHot": true,
        // 		"isDefault": true,
        // 		"addTime": "2022-12-29 20:40:51",
        // 		"updateTime": "2022-12-29 20:40:51"
        // 	},
        // 	"errmsg": "成功"
        // }
        MarketKeyword returnData = new MarketKeyword();
        returnData.setId(marketKeyword.getId());
        returnData.setKeyword(marketKeyword.getKeyword());
        returnData.setUrl(marketKeyword.getUrl());
        returnData.setIsHot(marketKeyword.getIsHot());
        returnData.setIsDefault(marketKeyword.getIsDefault());
        returnData.setAddTime(marketKeyword.getAddTime());
        returnData.setUpdateTime(marketKeyword.getUpdateTime());
        // 响应结果
        BaseRespVo baseRespVo = BaseRespVo.ok(returnData);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketKeyword keyword) {
        // 调用service 进行删除操作
        keywordService.delete(keyword);
        // 响应
        // {"errno":0,"errmsg":"成功"}
        return new BaseRespVo().ok("成功");
    }
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketKeyword keyword) {
        //  调用service更新, 入参,出参
        MarketKeyword keywordUpdate  = keywordService.update(keyword);
        return BaseRespVo.ok(keywordUpdate);
    }
}